home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / echo.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  23KB  |  843 lines

  1. /*
  2.  
  3.             Speak Freely for Unix
  4.                Echo Back Server
  5.  
  6.      Designed and implemented in January of 1996 by John Walker.
  7.  
  8. */
  9.  
  10. #include "speakfree.h"
  11. #include "version.h"
  12. #include "vat.h"
  13.  
  14. #define Echo_Base   0              /* Echo server offset from speaker port */
  15.  
  16. #define Echo_Time   10              /* Echo retransmit time in seconds */
  17.  
  18. static int debugforce = 0;          /* Debugging forced / prevented ? */
  19. static int whichport = Internet_Port + Echo_Base; /* Port to listen on (base address) */
  20. static int sock;              /* Input socket */
  21. static int ssock;              /* Control socket (RTP/VAT) */
  22. static struct sockaddr_in from;       /* Sending host address */
  23. static struct sockaddr_in name;       /* Address of destination host */
  24. static int fromlen;              /* Length of sending host address */
  25. static int showhosts = FALSE;          /* Show host names that connect */
  26. static int hosttimeout = 180 * 1000000L; /* Consider host idle after this time */
  27. static char *prog;              /* Program name */ 
  28. static long timebase;              /* Time in seconds at start of program */
  29.  
  30. static struct sockaddr_in lookhost;   /* Look who's listening host, if any */
  31. static char *sdes = NULL;          /* RTP SDES packet */
  32. static int sdesl;              /* RTP SDES packet length */
  33. static unsigned long ssrc;          /* RTP synchronisation source identifier */
  34. static double lwltimer;           /* Time next LWL retransmit scheduled */
  35. static int actives = 0;           /* Currently active hosts */
  36.  
  37. #define LWL_RETRANSMIT    (5 * 60 * 1000000.0) /* Microseconds between LWL updates */
  38.  
  39. static struct in_addr lwl_sites[LWL_MAX_SITES]; /* LWL site addresses */
  40. static long lwl_ports[LWL_MAX_SITES]; /* Ports for LWL hosts */
  41. static int lwl_nsites = 0;          /* Number of LWL sites published on */
  42.  
  43. /* Open connection state. */
  44.  
  45. #define connection e_connection
  46.  
  47. struct connection {
  48.     struct connection *con_next;      /* Next connection */
  49.     struct in_addr con_addr;          /* Host Internet address */
  50.     char pgpkey[17];              /* PGP key for connection */
  51.     char keymd5[16];              /* Digest of key file */
  52.     double con_timeout;           /* Connection timeout time */
  53.     char con_hostname[264];          /* Host name */
  54.     char *con_compmodes;          /* Last compression modes */
  55.     int con_control;              /* Control packet seen ? */
  56.     int con_bye;              /* BYE packet received ? */
  57.  
  58.     char face_filename[300];          /* Face temporary file name */
  59.     FILE *face_file;              /* Face file, when open */
  60.     pid_t face_viewer;              /* Face file viewer PID */
  61.     int face_stat;              /* Face retrieval status */
  62.     long face_address;              /* Address of current block request */
  63.     int face_retry;              /* Timeout retry count */
  64.     int face_timeout;              /* Timeout interval */
  65. };
  66.  
  67. struct connection *conn = NULL;       /* Chain of current connections */
  68.  
  69. #define Debug        (debugforce != 0) /* Generate debug output */
  70.  
  71. #define TickTock    (10 * 1000000L)   /* Alarm interval in microseconds */
  72. #define TockTock    (60 * 1000000L)   /* Alarm interval when no connections open */
  73.  
  74. struct queuedPacket {
  75.     struct queuedPacket *next;          /* Next packet in chain */
  76.     double when;              /* When packet should be sent */
  77.     struct sockaddr_in where;          /* Where packet should be sent */
  78.     int pktlen;               /* Packet length */
  79.     char pktdata[2];              /* Packet data */
  80. };
  81.  
  82. struct queuedPacket *qph = NULL,      /* Queue of packets waiting to be echoed */
  83.             *qptail = NULL;
  84. static int crit = FALSE;          /* Queue critical section lock */
  85. static int clash = FALSE;          /* Critical section clash retry flag */
  86.  
  87. /*  ETIME  --  Edit time and date for log messages.  */
  88.  
  89. static char *etime()
  90. {
  91.     struct tm *t;
  92.     time_t clock;
  93.     static char s[20];
  94.  
  95.     time(&clock);
  96.     t = localtime(&clock);
  97.     sprintf(s, "%02d-%02d %02d:%02d", t->tm_mon + 1, t->tm_mday,
  98.            t->tm_hour, t->tm_min);
  99.     return s;
  100. }
  101.  
  102. /*  COMPRESSIONTYPE  --  Return a string describing the type of
  103.              compression employed in this buffer.  We
  104.              attempt to distinguish RTP and VAT
  105.                          messages from our own here.  This isn't
  106.              100% reliable in the case of VAT, but
  107.              nothing really terrible will happen if
  108.              we guess wrong.  We only test a packet for
  109.                          VAT if given the clue that we've seen a
  110.              packet arrive on the control channel. */
  111.  
  112. static char *compressionType(msg, clue)
  113.   soundbuf *msg;
  114.   int clue;
  115. {
  116.     unsigned char *p = (unsigned char *) msg;
  117.  
  118.     if (((p[0] >> 6) & 3) == RTP_VERSION) {
  119.     char *r;
  120.  
  121.     switch (p[1] & 0x7F) {
  122.         case 0:
  123.                 r = "RTP PCMU";
  124.         break;
  125.  
  126.         case 1:
  127.                 r = "RTP 1016";
  128.         break;
  129.  
  130.         case 2:
  131.                 r = "RTP G721";
  132.         break;
  133.  
  134.         case 3:
  135.                 r = "RTP GSM";
  136.         break;
  137.  
  138.         case 5:
  139.         case 6:
  140.                 r = "RTP DVI4";
  141.         break;
  142.  
  143.         case 7:
  144.                 r = "RTP LPC";
  145.         break;
  146.  
  147.         case 8:
  148.                 r = "RTP PCMA";
  149.         break;
  150.  
  151.         case 9:
  152.                 r = "RTP G722";
  153.         break;
  154.  
  155.         case 10:
  156.         case 11:
  157.                 r = "RTP L16";
  158.         break;
  159.  
  160.         default:
  161.                 r = "RTP Unknown";
  162.         break;
  163.     }
  164.     return r;
  165.     }
  166.  
  167.     /* Hokey attempt to detect VAT packets.  We can't tell
  168.        VAT PCMU packets from unmarked Speak Freely packets, so
  169.        we let them fall through. */
  170.  
  171.     if (clue && (p[0] & 0xC0) == 0) {
  172.     char *r = NULL;
  173.  
  174.     switch (p[1] & 0x7F) {
  175.         case VAT_AUDF_GSM:
  176.                 r = "VAT GSM";
  177.         break;
  178.  
  179.         case VAT_AUDF_LPC4:
  180.                 r = "VAT LPC";
  181.         break;
  182.  
  183.         case VAT_AUDF_IDVI:
  184.                 r = "VAT IDVI";
  185.         break;
  186.  
  187.         case VAT_AUDF_L16_16:
  188.         case VAT_AUDF_L16_44:
  189.                 r = "VAT L16";
  190.         break;
  191.     }
  192.     if (r != NULL) {
  193.         return r;
  194.     }
  195.     }
  196.  
  197.     return ((msg->compression & (fComp2X | fCompGSM)) ==
  198.                 (fComp2X | fCompGSM)) ?
  199.                                 "GSM+2X compressed" :
  200.        ((msg->compression & (fComp2X | fCompADPCM)) ==
  201.                 (fComp2X | fCompADPCM)) ?
  202.                                 "ADPCM+2X compressed" :
  203.        ((msg->compression & (fComp2X | fCompLPC)) ==
  204.                 (fComp2X | fCompLPC)) ?
  205.                                 "LPC+2X compressed" :
  206.        ((msg->compression & (fComp2X | fCompLPC10)) ==
  207.                 (fComp2X | fCompLPC10)) ?
  208.                                 "LPC10+2X compressed" :
  209.        ((msg->compression & (fComp2X | fCompVOX)) ==
  210.                 (fComp2X | fCompVOX)) ?
  211.                                 "VOX+2X compressed" :
  212.            ((msg->compression & fCompADPCM) ? "ADPCM compressed" :
  213.            ((msg->compression & fCompLPC) ? "LPC compressed" :
  214.            ((msg->compression & fCompLPC10) ? "LPC10 compressed" :
  215.            ((msg->compression & fComp2X) ? "2X compressed" :
  216.            ((msg->compression & fCompGSM) ? "GSM compressed" :
  217.            ((msg->compression & fCompVOX) ? "VOX compressed" :
  218.                                             "uncompressed"))))));
  219. }
  220.  
  221. /*  MAKESESSIONKEY  --    Generate session key.  */
  222.  
  223. static void makeSessionKey(key)
  224.   char *key;
  225. {
  226.     struct MD5Context md5c;
  227.     char s[1024];
  228.  
  229.     s[0] = 0;
  230.     sprintf(s + strlen(s), "%u", getpid());
  231.     sprintf(s + strlen(s), "%u", getppid());
  232.     V getcwd(s + strlen(s), 256);
  233.     sprintf(s + strlen(s), "%u", clock());
  234.     V cuserid(s + strlen(s));
  235.     sprintf(s + strlen(s), "%u", time(NULL));
  236. #ifdef Solaris
  237.     sysinfo(SI_HW_SERIAL, s + strlen(s), 12);
  238. #else
  239.     sprintf(s + strlen(s), "%u", gethostid());
  240. #endif
  241.     getdomainname(s + strlen(s), 256);
  242.     gethostname(s + strlen(s), 256);
  243.     sprintf(s + strlen(s), "%u", getuid());
  244.     sprintf(s + strlen(s), "%u", getgid());
  245.     MD5Init(&md5c);
  246.     MD5Update(&md5c, s, strlen(s));
  247.     MD5Final(key, &md5c);
  248. }
  249.  
  250. /*  ULARM  --  Wrapper for setitimer() that looks like alarm()
  251.            but accepts a time in microseconds.  */
  252.  
  253. static void ularm(t)
  254.   long t;
  255. {
  256.     struct itimerval it;
  257.  
  258.     it.it_value.tv_sec = t / 1000000L;
  259.     it.it_value.tv_usec = t % 1000000L;
  260.     it.it_interval.tv_sec = it.it_interval.tv_usec = 0;
  261.     setitimer(ITIMER_REAL, &it, NULL);
  262. }
  263.  
  264. /*  SENDLWLMESSAGE  --    If enabled, send a message identifying us
  265.                         to each selected Look Who's Listening server.  */
  266.  
  267. static void sendLwlMessage(dobye)
  268.   int dobye;
  269. {
  270.     int i, sock;
  271.     struct sockaddr_in name;
  272.  
  273.     for (i = 0; i < lwl_nsites; i++) {
  274.     if (lwl_ports[i] >= 0) {
  275.         sock = socket(AF_INET, SOCK_STREAM, 0);
  276.         if (sock < 0) {
  277.                 perror("opening look who's listening socket");
  278.         sdes = NULL;
  279.         return;
  280.         }
  281.  
  282.         lookhost.sin_port = htons(lwl_ports[i]);
  283.         bcopy((char *) (&lwl_sites[i]), (char *) &lookhost.sin_addr.s_addr,
  284.           sizeof lookhost.sin_addr.s_addr);
  285.  
  286.         if (connect(sock, (struct sockaddr *) &(lookhost), sizeof lookhost) >= 0) {
  287.         if (dobye) {
  288.             char v[1024];
  289.             int l;
  290.  
  291.                     l = rtp_make_bye(v, ssrc, "Exiting sfspeaker", FALSE);
  292.             if (send(sock, v, l, 0) < 0) {
  293.                         perror("sending look who's listening BYE packet");
  294.             }
  295.         } else {
  296.             if (send(sock, (char *) sdes, sdesl, 0) < 0) {
  297.                         perror("sending look who's listening source ID message");
  298.             }
  299.         }
  300.         } else {
  301.                 perror("connecting look who's listening socket");
  302.         }
  303.     }
  304.     }
  305.     lwltimer = LWL_RETRANSMIT;
  306.     close(sock);
  307. }
  308.  
  309. /*  RELEASE  --  Alarm signal-catching function to retransmit
  310.          packets at the correct time and to time out
  311.          idle hosts.  */
  312.  
  313. static void release()
  314. {
  315.     struct connection *c, *l, *n;
  316.  
  317.     if (crit) {
  318.     clash = TRUE;
  319.     ularm(60000000L);
  320.     } else {
  321.     struct timeval tp;
  322.     struct timezone tzp;
  323.     struct queuedPacket *qp;
  324.     double td;
  325.     long timerStep;
  326.  
  327.     gettimeofday(&tp, &tzp);
  328.     td = (tp.tv_sec - timebase) * 1000000.0 + tp.tv_usec;
  329.  
  330.     /* Transmit any queued packets scheduled to go out within
  331.        epsilon of the current time. */
  332.  
  333. #define Timer_Epsilon    4000          /* Timer epsilon in microseconds */
  334. #define Timer_Latency    2000          /* Dispatch latency adjustment */
  335.  
  336. #if (Timer_Latency >= Timer_Epsilon)
  337.         error = "Timer epsilon must be greater than latency.";
  338. #endif
  339.  
  340.     while (qph != NULL && ((qph->when - td) < Timer_Epsilon)) {
  341.         if (qph->pktlen > 0) {
  342.         if (sendto(sock, qph->pktdata, qph->pktlen,
  343.             0, (struct sockaddr *) &(qph->where),
  344.             sizeof(struct sockaddr_in)) < 0) {
  345.                     perror("sending datagram message");
  346.         }
  347.         if (Debug) {
  348.                     fprintf(stderr, "%s: returning %d bytes to %s/%d.\n",
  349.             prog, qph->pktlen, inet_ntoa(qph->where.sin_addr),
  350.             ntohs(qph->where.sin_port));
  351.         }
  352.         }
  353.         qp = qph;
  354.         qph = qp->next;
  355.         if (qph == NULL) {
  356.         qptail = NULL;
  357.         }
  358.         free(qp);
  359.     }
  360.  
  361.         /* Mark idle any hosts that haven't sent us anything recently. */
  362.  
  363.     c = conn;
  364.     l = NULL;
  365.     actives = 0;
  366.     while (c != NULL) {
  367.         n = c->con_next;
  368.         if (!c->con_bye && (td < (c->con_timeout + hosttimeout))) {
  369.         actives++;
  370.         l = c;
  371.         } else {
  372.         if (showhosts) {
  373.                     fprintf(stderr, "%s: %s %s idle\n", prog, etime(), c->con_hostname);
  374.         }
  375.         if (l == NULL) {
  376.             conn = n;
  377.         } else {
  378.             l->con_next = n;
  379.         }
  380.         free(c);
  381.         }
  382.         c = n;
  383.     }
  384.  
  385.         /* Update our Look Who's Listening information if the
  386.        timeout has expired. */
  387.  
  388.     if (sdes != NULL && td >= lwltimer) {
  389.         sendLwlMessage(FALSE);
  390.         lwltimer = td + LWL_RETRANSMIT;
  391.     }
  392.  
  393.     /* Reset the time to the next event in the queue. */
  394.  
  395.     if (qph != NULL) {
  396.         timerStep = (long) ((qph->when - td) - Timer_Latency);
  397.     } else {
  398.         if (actives > 0) {
  399.         timerStep = TickTock;
  400.         } else {
  401.         timerStep = TockTock;
  402.         }
  403.     }
  404.     ularm(timerStep);
  405.     if (Debug) {
  406.             fprintf(stderr, "Tick: %.2f...\n", timerStep / 1000000.0);
  407.     }
  408.     }
  409.     signal(SIGALRM, release);          /* Set signal to handle timeout */
  410. }
  411.  
  412. /*  EXITING  --  Catch as many program termination signals as
  413.          possible and clean up before exit.  */
  414.  
  415. static void exiting()
  416. {
  417.  
  418.     if (sdes) {
  419.     sendLwlMessage(TRUE);
  420.     }
  421.     exit(0);
  422. }
  423.  
  424. /*  PROG_NAME  --  Extract program name from argv[0].  */
  425.  
  426. static char *prog_name(arg)
  427.   char *arg;
  428. {
  429.     char *cp = strrchr(arg, '/');
  430.  
  431.     return (cp != NULL) ? cp + 1 : arg;
  432. }
  433.  
  434. /*  USAGE  --  Print how-to-call information.  */
  435.  
  436. static void usage()
  437. {
  438.     V fprintf(stderr, "%s  --  Speak Freely echo server.\n", prog);
  439.     V fprintf(stderr, "            %s.\n", Relno);
  440.     V fprintf(stderr, "\n");
  441.     V fprintf(stderr, "Usage: %s [options]\n", prog);
  442.     V fprintf(stderr, "Options:\n");
  443.     V fprintf(stderr, "           -D               Enable debug output\n");
  444.     V fprintf(stderr, "           -Pport           Listen on given port\n");
  445.     V fprintf(stderr, "           -U               Print this message\n");
  446.     V fprintf(stderr, "           -Vtimeout        Show hostnames that connect\n");
  447.     V fprintf(stderr, "\n");
  448.     V fprintf(stderr, "by John Walker\n");
  449.     V fprintf(stderr, "   http://www.fourmilab.ch/\n");
  450. }
  451.  
  452. /*  Main program.  */
  453.  
  454. main(argc, argv)
  455.   int argc;
  456.   char *argv[];
  457. {
  458.     int i, length;
  459.     struct soundbuf sb;
  460.     struct connection *c;
  461.     char *cp;
  462.     int newconn;
  463.     struct auhdr {              /* .au file header */
  464.     char magic[4];
  465.     long hsize, dsize, emode, rate, nchan;
  466.     };
  467.     struct timeval tp;
  468.     struct timezone tzp;
  469.     struct queuedPacket *qp;
  470.  
  471.     prog = prog_name(argv[0]);
  472.     gettimeofday(&tp, &tzp);
  473.     timebase = tp.tv_sec;
  474.  
  475.     /* First pass option processing.  We have to first scan
  476.        the options to handle any which affect creation of the
  477.        socket.    One the second pass we can assume the socket
  478.        already exists, allowing us to join multicast groups,
  479.        etc. */
  480.  
  481.     for (i = 1; i < argc; i++) {
  482.     char *op, opt;
  483.  
  484.     op = argv[i];
  485.         if (*op == '-') {
  486.         opt = *(++op);
  487.         if (islower(opt)) {
  488.         opt = toupper(opt);
  489.         }
  490.  
  491.         switch (opt) {
  492.  
  493.                 case 'P':             /* -Pport  --  Port to listen on */
  494.             whichport = atoi(op + 1);
  495.             break;
  496.  
  497.                 case 'U':             /* -U  --  Print usage information */
  498.                 case '?':             /* -?  --  Print usage information */
  499.             usage();
  500.             return 0;
  501.         }
  502.     } else {
  503.         usage();
  504.         return 2;
  505.     }
  506.     }
  507.  
  508.     /* Create the sockets from which to read */
  509.  
  510.     sock = socket(AF_INET, SOCK_DGRAM, 0);
  511.     if (sock < 0) {
  512.         perror("opening data socket");
  513.     return 1;
  514.     }
  515.  
  516.     ssock = socket(AF_INET, SOCK_DGRAM, 0);
  517.     if (ssock < 0) {
  518.         perror("opening control socket");
  519.     return 1;
  520.     }
  521.  
  522.     /* Create name with wildcards. */
  523.  
  524.     name.sin_family = AF_INET;
  525.     name.sin_addr.s_addr = INADDR_ANY;
  526.     name.sin_port = htons(whichport);
  527.     if (bind(sock, (struct sockaddr *) &name, sizeof name) < 0) {
  528.         perror("binding data socket");
  529.     return 1;
  530.     }
  531.     name.sin_port = htons(whichport + 1);
  532.     if (bind(ssock, (struct sockaddr *) &name, sizeof name) < 0) {
  533.         perror("binding control socket");
  534.     return 1;
  535.     }
  536.     name.sin_port = htons(whichport);
  537.  
  538.     for (i = 1; i < argc; i++) {
  539.     char *op, opt;
  540.  
  541.     op = argv[i];
  542.         if (*op == '-') {
  543.         opt = *(++op);
  544.         if (islower(opt)) {
  545.         opt = toupper(opt);
  546.         }
  547.  
  548.         switch (opt) {
  549.  
  550.                 case 'D':             /* -D  --  Force debug output */
  551.             debugforce = 1;
  552.             break;
  553.  
  554.                 case 'V':             /* -V  --  Show hostnames that connect */
  555.             showhosts = TRUE;
  556.             if (op[1] != 0) {
  557.             int t = atoi(op + 1) * 1000000L;
  558.  
  559.             if (t > 0) {
  560.                 if (t < (TickTock + 1)) {
  561.                 t = TickTock + 1;
  562.                 }
  563.                 hosttimeout = (t / TickTock) * TickTock;
  564.             }
  565.             }
  566.             break;
  567.  
  568.         }
  569.     }
  570.     }
  571.  
  572.     /* Find assigned port value and print it. */
  573.  
  574.     length = sizeof(name);
  575.     if (getsockname(sock, (struct sockaddr *) &name, &length) < 0) {
  576.         perror("getting socket name");
  577.     return 1;
  578.     }
  579. #ifdef SHOW_SOCKET
  580.     fprintf(stderr, "%s: socket port #%d\n", prog, ntohs(name.sin_port));
  581. #endif
  582.  
  583.     /* Contact look who's listening host, if requested. */
  584.  
  585.     cp = getenv("SPEAKFREE_LWL_TELL");
  586.     if (cp != NULL) {
  587.     struct hostent *h;
  588.     char md5key[16];
  589.     char *ep, *np;
  590.     long iadr;
  591.     int n;
  592.  
  593.     makeSessionKey(md5key);
  594.     bcopy(md5key, (char *) &ssrc, sizeof ssrc);
  595.     sdesl = rtp_make_sdes(&sdes, ssrc, whichport, FALSE);
  596.     lookhost.sin_family = AF_INET;
  597.  
  598.     while (lwl_nsites < LWL_MAX_SITES) {
  599.         n = lwl_nsites;
  600.         while (*cp != 0 && isspace(*cp)) {
  601.         cp++;
  602.         }
  603.         if (*cp == 0) {
  604.         break;
  605.         }
  606.             if ((np = strchr(cp, ',')) != NULL) {
  607.         *np++ = 0;
  608.         }
  609.         lwl_ports[lwl_nsites] = Internet_Port + 2;
  610.             if ((ep = strchr(cp, ':')) != NULL) {
  611.         *ep = 0;
  612.         lwl_ports[lwl_nsites] = atoi(ep + 1);
  613.         }
  614.         if (isdigit(*cp) && (iadr = inet_addr(cp)) != -1) {
  615.         bcopy((char *) &iadr, (char *) (&lwl_sites[lwl_nsites]),
  616.               sizeof iadr);
  617.         lwl_nsites++;
  618.         } else {
  619.         h = gethostbyname(cp);
  620.         if (h != NULL) {
  621.             bcopy((char *) (h->h_addr), 
  622.               (char *) (&lwl_sites[lwl_nsites]),
  623.               sizeof(unsigned long));
  624.             lwl_nsites++;
  625.         } else {
  626.                     fprintf(stderr, "%s: warning, SPEAKFREE_LWL_TELL host %s unknown.\n",
  627.             prog, cp);
  628.         }
  629.         }
  630.         if (Debug && lwl_nsites > n) {
  631.                 fprintf(stderr, "%s: publishing on LWL server %s: %s.\n", prog,
  632.             inet_ntoa(lwl_sites[n]), cp);
  633.         }
  634.         if (np == NULL) {
  635.         break;
  636.         }
  637.         cp = np;
  638.     }
  639.     if (lwl_nsites > 0) {
  640.         sendLwlMessage(FALSE);
  641.     }
  642.     }
  643.  
  644.     signal(SIGHUP, exiting);          /* Set signal to handle termination */
  645.     signal(SIGINT, exiting);          /* Set signal to handle termination */
  646.     signal(SIGTERM, exiting);          /* Set signal to handle termination */
  647.     signal(SIGALRM, release);          /* Set signal to handle timeout */
  648.     ularm(TockTock);              /* Start periodic timer */
  649.  
  650.     /* Read from the socket. */
  651.  
  652.     while (TRUE) {
  653.     int rll;
  654.     double pktime;              /* Time packet is to be echoed */
  655.     fd_set fdset;
  656.     int wsock, control;
  657.     unsigned char *p = (unsigned char *) &sb;
  658.  
  659.     FD_ZERO(&fdset);
  660.     FD_SET(sock, &fdset);
  661.     FD_SET(ssock, &fdset);
  662.     if (select(FD_SETSIZE, &fdset, NULL, NULL, NULL) <= 0) {
  663.         continue;
  664.     }
  665.     wsock = FD_ISSET(sock, &fdset) ? sock :
  666.             (FD_ISSET(ssock, &fdset) ? ssock : -1);
  667.     if (wsock < 0) {
  668.         continue;
  669.     }
  670.     control = wsock == ssock;
  671.     fromlen = sizeof(from);
  672.     if ((rll = recvfrom(wsock, (char *) &sb, sizeof sb, 0, (struct sockaddr *) &from, &fromlen)) < 0) {
  673.         if (errno != EINTR) {
  674.                 perror(!control ? "receiving data packet" :
  675.                                   "receiving control packet");
  676.         }
  677.         continue;
  678.     }
  679.     gettimeofday(&tp, &tzp);      /* Get time packet received */
  680.     pktime = ((tp.tv_sec + Echo_Time) - timebase) * 1000000.0 + tp.tv_usec;
  681. #ifndef SHOW_SOCKET
  682.     if (Debug) {
  683.             fprintf(stderr, "%s: %d bytes read from %s socket.\n", prog, rll,
  684.                     control ? "control" : "data");
  685.     }
  686. #endif
  687.  
  688.     /* See if this connection is active.  If not, initialise a new
  689.        connection. */
  690.  
  691.     newconn = FALSE;
  692.     crit = TRUE;              /* Set connection list critical section lock */
  693.     c = conn;
  694.     while (c != NULL) {
  695.         if (memcmp(&from.sin_addr, &(c->con_addr),
  696.                sizeof(struct in_addr)) == 0) {
  697.         break;
  698.         }
  699.         c = c->con_next;
  700.     }
  701.     if (c == NULL) {
  702.         c = (struct connection *) malloc(sizeof(struct connection));
  703.         if (c != NULL) {
  704.         struct hostent *h;
  705.  
  706.         newconn = TRUE;
  707.         c->con_next = conn;
  708.         c->pgpkey[0] = FALSE;
  709.         bzero(c->keymd5, 16);
  710.         conn = c;
  711.         bcopy(&from.sin_addr, &(c->con_addr),
  712.             sizeof(struct in_addr));
  713.         h = gethostbyaddr((char *) &from.sin_addr, sizeof(struct in_addr),
  714.                   AF_INET);
  715.         if (h == NULL) {
  716.             strcpy(c->con_hostname, inet_ntoa(from.sin_addr));
  717.         } else {
  718.             strcpy(c->con_hostname, h->h_name);
  719.         }
  720.         }
  721.     } else if (c->con_timeout == -1) {
  722.         newconn = TRUE;
  723.     }
  724.  
  725.     /* Initialise fields in connection.  Only fields which need to
  726.        be reinitialised when a previously idle host resumes activity
  727.        need be set here. */
  728.  
  729.     if (newconn) {
  730.         c->face_file = NULL;
  731.         c->face_filename[0] = 0;
  732.         c->face_viewer = 0;
  733.         c->face_stat = FSinit;
  734.         c->face_address = 0L;
  735.         c->face_retry = 0;
  736.         c->con_compmodes = NULL;
  737.         c->con_control = FALSE;
  738.         c->con_bye = FALSE;
  739.     }
  740.  
  741.     if (c != NULL) {
  742.         /* Reset connection timeout. */
  743.         c->con_timeout = pktime;
  744.         if (newconn || c->con_bye) {
  745.         if (showhosts) {
  746.                     fprintf(stderr, "%s: %s %s connect\n", prog, etime(), c->con_hostname);
  747.         }
  748.         c->con_bye = FALSE;
  749.         }
  750.     } else {
  751.         crit = FALSE;
  752.         if (clash) {
  753.         release();
  754.         }
  755.         continue;
  756.     }
  757.  
  758.         /* Detect BYE packets and don't retransmit them, as they'll
  759.        reopen the connection on the other end. */
  760.  
  761.     if (control && ((isValidRTCPpacket((unsigned char *) &sb, rll) &&
  762.              isRTCPByepacket((unsigned char *) &sb, rll)) ||
  763.             (((p[0] & 0xC0) == 0) && (p[1] == 2)))) {
  764.         struct queuedPacket *qp;
  765.  
  766.         if (showhosts) {
  767.                 fprintf(stderr, "%s: %s %s bye\n", prog, etime(), c->con_hostname);
  768.         }
  769.         c->con_bye = TRUE;
  770.  
  771.         /* Discard any packets queued to this host. */
  772.  
  773.         qp = qph;
  774.         while (qp != NULL) {
  775.         if (memcmp(&(qp->where.sin_addr), &(c->con_addr),
  776.                sizeof(struct in_addr)) == 0) {
  777.             if (Debug) {
  778.                         fprintf(stderr, "%s: discarding %d bytes to %s/%d.\n",
  779.                 prog, qph->pktlen, inet_ntoa(qph->where.sin_addr),
  780.                 ntohs(qph->where.sin_port));
  781.             }
  782.             qp->pktlen = 0;
  783.         }
  784.         qp = qp->next;
  785.         }
  786.         crit = FALSE;
  787.         if (clash) {
  788.         release();
  789.         }
  790.         continue;
  791.     }
  792.  
  793.     /* Add the packet to the queue of packets awaiting
  794.        retransmission. */
  795.  
  796.     qp = (struct queuedPacket *) malloc(sizeof(struct queuedPacket) + rll);
  797.     if (qp != NULL) {
  798.         qp->next = NULL;
  799.         qp->when = pktime;
  800.         bcopy((char *) &from, (char *) &(qp->where),
  801.         sizeof(struct sockaddr_in));
  802.         qp->where.sin_port = htons(whichport + (control ? 1 : 0));
  803.         qp->pktlen = rll;
  804.         bcopy((char *) &sb, qp->pktdata, rll);
  805.         if (qptail == NULL) {
  806.         qph = qptail = qp;
  807.         ularm(Echo_Time * 1000000L);
  808.         } else {
  809.         qptail->next = qp;
  810.         qptail = qp;
  811.         }
  812.     }
  813.  
  814.     c->con_control |= control;
  815.     if (showhosts && !control) {
  816.         char *cmodes = compressionType(&sb, c->con_control);
  817.  
  818.         if (c->con_compmodes != cmodes) {
  819.         c->con_compmodes = cmodes;
  820.                 fprintf(stderr, "%s: %s sending %s.\n", prog, c->con_hostname,
  821.             cmodes);
  822.         }
  823.     }
  824.     if (Debug) {
  825.             fprintf(stderr, "%s: echoing %d %s bytes from %s.\n",
  826.             prog, rll,
  827.                     control ? "RTP/VAT control" :
  828.                   compressionType(&sb, c->con_control),
  829.             c->con_hostname);
  830.     }
  831.  
  832.     crit = FALSE;
  833.     if (clash) {
  834.         release();
  835.     }
  836.     }
  837. #ifdef MEANS_OF_EXIT
  838.     close(sock);
  839.     exiting();
  840.     return 0;
  841. #endif
  842. }
  843.